#include "Friend.h"
#include "string"
#include "iostream"
CFriend::CFriend(void)
{
}


CFriend::~CFriend(void)
{//need to be made
}
void CFriend::Add_Friend   (string username_,string friendname_,multimap<string, string> &friend_list,multimap<string, string> &requests)
{
		if(Is_Friend(username_,friendname_,friend_list)==0&&Is_Friend( friendname_,username_,friend_list)==0)
		{
			friend_list	.insert(pair<string, string>( username_, friendname_));
			requests	.insert(pair<string,string>(friendname_,username_));
		}
		else if(Is_Friend( username_, friendname_,friend_list)==0&&Is_Friend( friendname_,username_,friend_list)==1)
		{
			friend_list.insert(pair<string, string>( username_, friendname_));
			Remove_Request( username_,friendname_,requests);
		}
	
}
void CFriend::cancel_request(string username_,string friendname_,multimap<string, string> &friend_list,multimap<string, string> &requests)
{
	Remove_Request	(username_,friendname_,requests);
	Remove_Friend	(friendname_,username_,friend_list,requests);
}
void CFriend::Remove_Request(string username_,string friendname_,multimap<string, string> &requests)
{
	//multimap<char,int>::iterator it ;
    std::pair <std::multimap<string,string>::iterator, std::multimap<string,string>::iterator> ret;
    ret = requests.equal_range(username_);
    
    for (multimap<string,string>::iterator it=ret.first; it!=ret.second; ++it)
		if((it)->second == friendname_)
		{
			requests.erase(it);
			break;
		}
}	

/*bool CFriend::Is_User(string username_,multimap<string, string> &friend_list)
{
	if(friend_list.find(username_)==friend_list.end())return false;
	return true;
}*/
void CFriend::Remove_Friend(string username_,string friendname_,multimap<string, string> &friend_list,multimap<string, string> &requests)
{
	Remove( username_ ,friendname_,friend_list,requests);
	Remove(friendname_,username_ ,friend_list,requests );
}

void CFriend::Remove(string username_,string friendname_,multimap<string, string> &friend_list,multimap<string, string> &requests)//remove from friend list and request
{
	//multimap<char,int>::iterator it ;
    std::pair <std::multimap<string,string>::iterator, std::multimap<string,string>::iterator> ret;
    ret = friend_list.equal_range(username_);
    
    for (std::multimap<string,string>::iterator it=ret.first; it!=ret.second; ++it)
		if((it)->second == friendname_)
		{
			friend_list.erase(it);
			//cout<<"friend erased successfully/n";
			break;
		}
	Remove_Request( username_,friendname_,requests);
}	
bool CFriend::Is_Friend(string username_,string friendname_,multimap<string, string> &friend_list)
{
	std::pair <std::multimap<string,string>::iterator, std::multimap<string,string>::iterator> ret;
	ret = friend_list.equal_range(username_);
    for (std::multimap<string,string>::iterator it=ret.first; it!=ret.second; ++it)
		if((it)->second == friendname_)
		{
			return true;
			//cout<<"friend erased successfully/n";
		}
		return false;
}
string  CFriend::Show_request  (string username_,multimap<string, string> &requests)
{
	string friendrequest;
	std::pair <std::multimap<string,string>::iterator, std::multimap<string,string>::iterator> ret;
    ret = requests.equal_range(username_);
    for (std::multimap<string,string>::iterator it=ret.first; it!=ret.second; ++it)
	{
		friendrequest += ' '        ;
		friendrequest += (it->second);
	}
	friendrequest += ' '        ;
	return friendrequest;
}//,friend_list,requests
string  CFriend::Show_Friend  (string username_,multimap<string, string> &friend_list)
{
	string friendlist;
	std::pair <std::multimap<string,string>::iterator, std::multimap<string,string>::iterator> ret;
    ret = friend_list.equal_range(username_);
    for(std::multimap<string,string>::iterator it=ret.first; it!=ret.second; ++it)
	{
		friendlist += ' '        ;
		friendlist += (it->second);
	}
	friendlist += ' '        ;
	return friendlist;
}